//
//  ViewController.m
//  ZHYYView
//
//  Created by yuan zhang on 2021/9/15.
//  Copyright © 2021 yuan zhang. All rights reserved.
//
#import <ZHYYTool/ZHYYTool.h>
#import "ViewController.h"
#import "ZHYYbutton.h"
#import "ZHYYTable.h"
#import "OutViewTest.h"
#import "Feed.h"
#import "FeedItem.h"

#import "ZHYYPopBtn.h"
#import "ZHYYTextView.h"

#import "ContentViewController.h"
#import "ZTPopup.h"

#import "ZTPanelWindow/ZTPanelWindow.h"
#import "ZTAlert.h"

@implementation ViewController

{
    ZHYYbutton *btn1,*btn2,*btn3,*btn4,*btn5;
    ZHYYTable *tb;
    NSNotificationCenter *center;
    OutViewTest *ou;
    ZHYYPopBtn *pop;
    ZHYYTextView *textv;
    BOOL isRun;
    ZTPopup *popOver;
    ContentViewController *contentVC;
    ZTPanelWindow *zp;
    NSImageView *imageView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setAutoresizesSubviews:YES];
    btn1 =[self loadbtnWithFrame:CGRectMake(10, 300, 64, 24) Title:@"popover" Action:@selector(btnCilck1)];
    btn1.status=YES;
    btn1.defaultBackgroundColor =[NSColor greenColor];
    
    btn2 =[self loadbtnWithFrame:CGRectMake(80, 285, 60, 60) Title:@"popwindow"
        Action:@selector(btnCilck2)];
    btn2.defaultBackgroundColor =[NSColor systemYellowColor];
    btn2.radius =30;
    
    btn3 =[self loadbtnWithFrame:CGRectMake(350, 300, 60, 24) Title:@"diasble" Action:@selector(btnCilck3)];
    btn3.defaultBackgroundColor =[NSColor purpleColor];
    btn3.radius =12;
    
    btn4 =[self loadbtnWithFrame:CGRectMake(150, 285, 60, 60) Title:@"alertWindow" Action:@selector(btnCilck4)];
    btn4.defaultBackgroundColor =[NSColor systemBrownColor];
    btn4.radius =0;
    
    btn5 =[self loadbtnWithFrame:CGRectMake(220, 300, 100, 24) Title:@"alertSheet" Action:@selector(btnCilck5)];
    btn5.radius =0;
    
    [self.view addSubview:btn1];
    [self.view addSubview:btn2];
    [self.view addSubview:btn3];
    [self.view addSubview:btn4];
    [self.view addSubview:btn5];
    
    tb =[[ZHYYTable alloc] initWithFrame:CGRectMake(20, 20, 520, 240) ColumnNumber:4];
    [tb setAutoresizingMask:NSViewWidthSizable];
    [self.view addSubview:tb];

    ou =[[OutViewTest alloc] initWithFrame:CGRectMake(20, 360, 1000, 400)];
    //设置视图是否可以随着窗口大小变动进行调整
//    [ou setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
    [self.view addSubview:ou];
    
//    pop =[[ZHYYPopBtn alloc] initWithFrame:CGRectMake(300, 240, 300, 120)];
//    [self.view addSubview:pop];
    
    textv =[[ZHYYTextView alloc]initWithFrame:CGRectMake(580, 20, 450, 238)];
    textv.textEditable = NO;
    textv.textSelectable = YES;
    [textv setMessage:@"This application is trying to draw a very large com" Cover:YES];
    NSLog(@"%@",[textv message]);
    [self.view addSubview:textv];
    
    [self creteImageView];
    _progress.doubleValue=0;
    _baifen.stringValue =[NSString stringWithFormat:@"%.2f",_progress.doubleValue];
//    CGRect tempFrame = _progress.frame;
//    tempFrame.size.height = _progress.frame.size.height * 1.5;
//    _progress.frame = tempFrame;
    _cpp2.doubleValue = 0;
    _cpp2.displayedWhenStopped =NO;
//    _cpp2.usesThreadedAnimation=YES;
    
    
    zp =[[ZTPanelWindow alloc]init];
    [zp.window center];
    [zp.window orderFront:nil];

    
    
    
    [self createPopover];
    
    [self showtime];
    [self processTest];
    [self tbTest];

    
    // Do any additional setup after loading the view.
    center =[NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(showALlView:) name:@"ShowView" object:nil];
    [center addObserver:self selector:@selector(popOvertest:) name:@"pop" object:nil];
}



-(void)popOvertest:(NSNotification*)noti{
    NSString *ip = [NSString stringWithFormat:@"%@",[noti.userInfo objectForKey:@"ip"]];
    NSString *port = [NSString stringWithFormat:@"%@",[noti.userInfo objectForKey:@"port"]];
        NSLog(@"key info:%@---%@",ip,port);
}


- (void)createPopover
{
    // will be shown inside the popover
    contentVC = [[ContentViewController alloc] initWithNibName:@"ContentViewController" bundle:nil];
    
    popOver = [[ZTPopup alloc] initWithViewController:contentVC ];
    popOver.animated=NO;
    contentVC.popup= popOver;
}





-(void)creteImageView{
    imageView = [[NSImageView alloc]initWithFrame:CGRectMake(600, 270, 50, 64)];
    imageView.image =[NSImage imageNamed:@"greenLed.png"];
    [self.view addSubview:imageView];
}


-(void)showtime{
        __block int i =0;
        dispatch_queue_t queue =dispatch_queue_create("queue", DISPATCH_QUEUE_SERIAL);
        dispatch_async(queue, ^{
            while (YES) {
                usleep(1000*1000);
                i++;
                dispatch_sync(dispatch_get_main_queue(), ^{
                    self->_timer.stringValue =[ZTDateHandle formatTimeString:@"E HH:mm:ss"];
                    if (i%2 == 0) {
                        self->imageView.image =[NSImage imageNamed:@"redLed.png"];
                    }else {
                        self->imageView.image =[NSImage imageNamed:@"greenLed.png"];
                    }
                });
             }
        });
}


-(void)testQueue2{
        dispatch_queue_t queue =dispatch_queue_create("queue", DISPATCH_QUEUE_SERIAL);
        dispatch_async(queue, ^{
            while (YES) {
                NSLog(@"add >>>>>");
                int i =(arc4random() % 10) + 1;
                usleep(1000*500);
                dispatch_sync(dispatch_get_main_queue(), ^{
                    self->_progress.doubleValue = self->_progress.doubleValue+i;
                    self->_baifen.stringValue = [NSString stringWithFormat:@"%.2f", self->_progress.doubleValue];
                });
             }
        });
}


-(void)processTest{
    isRun =YES;
     [_cpp2 startAnimation:nil];
    dispatch_queue_t queue =dispatch_queue_create("queue", DISPATCH_QUEUE_SERIAL);
    dispatch_async(queue, ^{
        while (self->isRun) {
            // 执行子线程任务
            usleep(1000*500);
            dispatch_async(dispatch_get_main_queue(), ^{
                
//                [self->ou changeDUT:1 Data:@"fail" ID:@"1" SubID:@"1.1"];
//                [self->ou changeDUT:1 Data:@"pass" ID:@"1" SubID:@"1.2"];
//                [self->ou changeDUT:1 Data:@"ok" ID:@"1" SubID:@"1.3"];
//                [self->ou changeDUT:1 Data:@"error" ID:@"1" SubID:@"1.4"];
//                [self->ou changeDUT:1 Data:@"error" ID:@"1" SubID:@"1.5"];
                // 追加在主线程中执行的任务
                if (self->_progress.doubleValue < 100.00 ) {
                    int i =(arc4random() % 17) + 1;
                    double j = (double)i/7;
//                    NSLog(@"%f",j);
                    self->_progress.doubleValue =self->_progress.doubleValue +j;
                    self->_cpp2.doubleValue = self->_cpp2.doubleValue +j;

                    self->_baifen.stringValue = [NSString stringWithFormat:@"%.2f", self->_progress.doubleValue];
//                        NSLog(@"loading... %d",self->isRun);
//                    [self->textv setMessage:[NSString stringWithFormat: @"2021-09-30 08:12:21.580286+0800 ZHYYView[818:16554] loading...%.2f %%",self->_progress.doubleValue] Cover:NO];
                    
                    [self->textv addMessage:[NSString stringWithFormat: @"loading...%.2f %%",self->_progress.doubleValue]];
                    
                } if (self->_progress.doubleValue >= 100.00  && self->isRun) {
//                    NSLog(@"%.2f",self->_progress.doubleValue);
//                    NSLog(@"complete >>>>> %@",self->isRun?@"True":@"False");
//                    [self->textv addMessage:@"complete >>>>>"];
//                    [self->textv clearMessage];
                    [self->textv addMessage:@"complete >>>>>"];
                    [self->_cpp2 stopAnimation:nil];
                    self->isRun =NO;
                }
            });
        }
    });
}



-(void)tbTest{
    NSInteger count = [tb tableRowCount];
     [_cpp2 startAnimation:nil];
    dispatch_queue_t queue =dispatch_queue_create("queue", DISPATCH_QUEUE_SERIAL);
    dispatch_async(queue, ^{
        for (int i=0; i <count; i++) {
            // 执行子线程任务
            usleep(1000*1000);
            dispatch_async(dispatch_get_main_queue(), ^{
                [self->tb changeDUT1Data:@"pass" atRowIndex:i];
                [self->tb changeDUT2Data:@"fail" atRowIndex:i];
//                [self->tb reloadDataTB];
                NSIndexSet *indexSet1 = [[NSIndexSet alloc] initWithIndexesInRange: NSMakeRange(0, i+1)];
                NSIndexSet *indexSet2 = [[NSIndexSet alloc] initWithIndexesInRange: NSMakeRange(2,3)];
                [self->tb reloadDataWithRowIndex:indexSet1 columnIndex:indexSet2];
                [self->tb testScroll: i];
                
            });
        }
    });
}







- (void)setRepresentedObject:(id)representedObject {
    [super setRepresentedObject:representedObject];

    // Update the view, if already loaded.
}


-(void)reloadout{
    [ou changeDUT:1 Data:@"fail" ID:@"1" SubID:@"1.1"];
    [ou reloadDataOut];
}



-(void)showALlView:(NSNotification *)noti{
    NSString *keyInfo = [NSString stringWithFormat:@"%@",[noti.userInfo objectForKey:@"key"]];
    NSLog(@"key info:%@",keyInfo);
    btn1.hidden=NO;
    btn2.hidden=NO;
    btn3.hidden=NO;
    tb.hidden=NO;
}


-(ZHYYbutton*)loadbtnWithFrame:(CGRect)frame Title:(NSString*)title Action:(SEL)action{
    ZHYYbutton *btn = [[ZHYYbutton alloc] initWithFrame:frame];
    btn.isHandCursor = YES;
    btn.defaultTitle = title;
    btn.selectedTitle = title;
    btn.defaultTitleColor = [NSColor whiteColor];
    btn.selectedTitleColor = [NSColor blackColor];
    btn.defaultFont = [NSFont systemFontOfSize:14];
    btn.selectedFont = [NSFont systemFontOfSize:14];
    btn.defaultBackgroundColor = [NSColor systemBlueColor];
    btn.selectedBackgroundColor = [NSColor whiteColor];
    btn.defaultBackgroundImage = [NSImage imageNamed:@""];
    btn.selectedBackgroundImage = [NSImage imageNamed:@""];
    btn.rectCorners = ZHRectCornerTopLeft|ZHRectCornerBottomLeft|ZHRectCornerTopRight|ZHRectCornerBottomRight;
    btn.radius =4.5;
    btn.textAlignment = ZHTextAlignmentCenter;
    btn.textUnderLineStyle =ZHTextUnderLineStyleNone;
    [btn setTarget:self];
    [btn setAction:action];
    btn.status =YES;
    btn.disabledTitleColor=btn.defaultTitleColor;
    btn.disabledBackgroundColor=[NSColor redColor];
    
    return btn;
}



-(void)btnCilck1{
    [popOver showPopover:btn1];
}


-(void)btnCilck2{
    [zp showPanel:self.view.window];
}


-(void)btnCilck3{
    if (btn1.status == YES && btn2.status ==YES && btn4.status == YES && btn5.status ==YES) {
        btn1.status=NO;
        btn2.status =NO;
        btn4.status =NO;
        btn5.status =NO;
        btn3.defaultTitle =@"enable";
    }else{
        btn1.status=YES;
        btn2.status =YES;
        btn4.status=YES;
        btn5.status =YES;
        btn3.defaultTitle =@"disable";
    }
}

-(void)btnCilck4{
    [ZTAlert ShowAlertWindow];
    
}

-(void)btnCilck5{
    [ZTAlert ShowAlertSheet];
}

-(void)load1{
    
    NSAlert *alert = [[NSAlert alloc] init];
    alert.alertStyle = NSAlertStyleCritical;
    [alert addButtonWithTitle:@"确定"];
    [alert addButtonWithTitle:@"取消"];

        alert.messageText = @"Load New Menu";
        alert.informativeText = @"ary you sure?";

        [alert beginSheetModalForWindow:[NSApplication sharedApplication].keyWindow completionHandler:^(NSModalResponse returnCode) {
            if (returnCode == NSAlertFirstButtonReturn) {
                NSLog(@"确定");

            } else if (returnCode == NSAlertSecondButtonReturn) {
                NSLog(@"取消");

            } else {
                NSLog(@"else");
            }
        }];

}
@end
